﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FantastPlatform.Community.IMode;
using FantastPlatform.Community.IService;
using FantastPlatform.Framework;
using FantastPlatform.Community.Model;

namespace FantastPlatform.Community.UI.Service
{
    public partial class PersonTypeService : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            IPersonTypeService service = ServiceFactory.CreateService<IPersonTypeService>();
            int totalCount = 0;
            switch (Action)
            {
                case "Search":
                    IList<IPersonType> placeTypeList = service.LoadList(null, PageSize, SelfPage, out totalCount);
                    ReturnList(totalCount, placeTypeList);
                    break;
                case "Add":
                    string data = Data;
                    IPersonType placeType = JsonToEntity<PersonType>(RepairString(data));
                    if (placeType.Id == 0)
                    {
                        placeType.CreateDate = DateTime.Now;
                        placeType.Enable = true;
                        service.Create(placeType);
                        ReturnSuccessMessage("添加场所类型成功");
                    }
                    else
                    {
                        IPersonType placeType2 = service.Get(placeType.Id);
                        placeType2.Name = placeType.Name;
                        placeType2.Code = placeType.Code;
                        placeType2.Description = placeType.Description;

                        placeType2.ModifyDate = DateTime.Now;
                        service.Update(placeType2);
                        ReturnSuccessMessage("修改场所类型成功");
                    }
                    break;
                case "Delete":
                    if (string.IsNullOrEmpty(Data))
                    {
                        throw new Exception("传入的场所类型ID为空");
                    }
                    List<int> idList = new List<int>();
                    string[] idStrList = Data.Split(',');
                    foreach (string id in idStrList)
                    {
                        idList.Add(int.Parse(id));
                    }
                    service.Delete(idList);
                    ReturnSuccessMessage("删除成功");
                    break;
                case "Get":
                    placeType = service.Get(Id);
                    ReturnObj(placeType);
                    break;
            }
        }
    }
}